package com.javatian.controller;

import com.github.pagehelper.PageInfo;
import com.javatian.common.Result;
import com.javatian.entity.Account;
import com.javatian.entity.GoodsInfo;
import com.javatian.service.GoodsInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品信息控制器
 */
@RestController
@RequestMapping(value = "/goodsInfo")
public class GoodsInfoController {

    //用户信息
    @Value("${const.user}")
    private String user;

    @Resource
    private GoodsInfoService goodsInfoService;


    /**
     * 新增一个商品信息
     */
    @PostMapping
    public Result<GoodsInfo> add(@RequestBody GoodsInfo goodsInfo,HttpServletRequest request){
        Account account = (Account) request.getSession().getAttribute(user);
        goodsInfo.setUserId(account.getId());
        goodsInfo.setLevel(account.getLevel());
        goodsInfoService.add(goodsInfo);
        return Result.success(goodsInfo);
    }

    /**
     * 根据主键id删除一个商品信息
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        goodsInfoService.delete(id);
        return Result.success();
    }

    /**
     * 修改一个商品信息
     */
    @PutMapping
    public Result update(@RequestBody GoodsInfo goodsInfo){
        goodsInfoService.update(goodsInfo);
        return Result.success();
    }

    /**
     * 根据主键id获取一个商品信息对象
     */
    @GetMapping("/{id}")
    public Result<GoodsInfo> detail(@PathVariable Long id){
        GoodsInfo goodsInfo = goodsInfoService.findById(id);
        return Result.success(goodsInfo);
    }

    /**
     * 查询所有商品信息记录
     */
    @GetMapping
    public Result<List<GoodsInfo>> all(){
        return Result.success(goodsInfoService.findAll());
    }

    /**
     * 根据条件分页查询
     * @param name 名称
     * @param pageNum 第几页
     * @param pageSize 每页数量
     * @return 当前页的List列表
     */
    @GetMapping("/page/{name}")
    public Result<PageInfo<GoodsInfo>> page(@PathVariable String name,
                                            @RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "5") Integer pageSize,
                                            HttpServletRequest request){
        Account account = (Account) request.getSession().getAttribute(user);
        return Result.success(goodsInfoService.findPage( pageNum, pageSize,name,account));
    }

    /**
     * 根据商品获取热门商品
     */
    @GetMapping("/findByType/{typeId}")
    public Result<List<GoodsInfo>> findByType(@PathVariable Integer typeId){

        return Result.success( goodsInfoService.findByType(typeId));

    }

    /**
     * 推荐商品
     */
    @GetMapping("/recommend")
    public Result<PageInfo<GoodsInfo>> recommend( @RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize){

        return Result.success( goodsInfoService.findRecommendGoodsInfos(pageNum,pageSize));

    }

    /**
     * 查询用户购买过的所有商品
     */
    @GetMapping("/comment/{userId}/{level}")
    public Result<List<GoodsInfo>> orderGoods(@PathVariable ("userId")Long userId,@PathVariable("level")Integer level){
        return Result.success(goodsInfoService.getOrderGoods(userId,level));
    }



}


















